package com.payment.ktb.activity.main4;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.payment.ktb.Interface.HttpCallbackListener;
import com.payment.ktb.R;
import com.payment.ktb.activity.BaseActivity;
import com.payment.ktb.activity.main1.OrderConfirmActivity;
import com.payment.ktb.activity.main1.OrderConfirmBuyMembershipActivity;
import com.payment.ktb.adapter.MembershipCenterAdapter;
import com.payment.ktb.constants.ConstantsPay;
import com.payment.ktb.constants.HttpActions;
import com.payment.ktb.constants.HttpUrls;
import com.payment.ktb.model.VipMealEntity;
import com.payment.ktb.utils.AlertDialogUtils;
import com.payment.ktb.utils.HttpUtils;
import com.payment.ktb.utils.LogUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by admin on 2017/9/5.
 */

public class MembershipCenterActivity extends BaseActivity {


    @BindView(R.id.tv_membershipcenter_amount)
    TextView tv_membershipcenter_amount;
    @BindView(R.id.tv_membershipcenter_vipRate)
    TextView tv_membershipcenter_vipRate;
    @BindView(R.id.tv_membershipcenter_birthdayCount)
    TextView tv_membershipcenter_birthdayCount;
    @BindView(R.id.tv_membershipcenter_vipnotes)
    TextView tv_membershipcenter_vipnotes;
    @BindView(R.id.tv_membershipcenter_viptime)
    TextView tv_membershipcenter_viptime;
    @BindView(R.id.lv_memershipcenter)
    ListView lv_memershipcenter;
    @BindView(R.id.btn_membershipcenter_payment_confirm)
    Button btn_membershipcenter_payment_confirm;

    String linkId;
    List<VipMealEntity> listDatas;
    MembershipCenterAdapter adapter;
    String amount;
    private String goodsName;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_membershipcenter);
        ButterKnife.bind(this);
        initTitle("会员中心");
        initData();
    }

    @Override
    protected void onResume() {
        super.onResume();
       linkId = String.valueOf(new Date().getTime());
    }

    private void initData() {
        tv_membershipcenter_vipRate.setText(getIntent().getStringExtra("vipRate") + "");
        tv_membershipcenter_birthdayCount.setText("送" + getIntent().getStringExtra("birthdayCount") + "张抵用券");
        if (getIntent().getStringExtra("isVip").equals("0")) {
            tv_membershipcenter_vipnotes.setText("开通会员，享受专属特权");
            tv_membershipcenter_viptime.setText("会员未开通");
            btn_membershipcenter_payment_confirm.setText("立即开通");
        }else {
            tv_membershipcenter_vipnotes.setText("会员专属特权");
            tv_membershipcenter_viptime.setText(getIntent().getStringExtra("expireDay") + "天后会员到期");
            btn_membershipcenter_payment_confirm.setText("立即续费");
        }
        //data
        listDatas = new Gson().fromJson(getIntent().getStringExtra("meal"), new TypeToken<List<VipMealEntity>>() {}.getType());
        if (listDatas != null && !listDatas.isEmpty()) {
            listDatas.get(0).setSelect(true);
            amount = listDatas.get(0).getPrice();
            tv_membershipcenter_amount.setText("¥ " + Integer.valueOf(amount)/100);
            goodsName = listDatas.get(0).getCode();
        }
        //adapter
        adapter = new MembershipCenterAdapter(mContext,listDatas);

        //bind
        lv_memershipcenter.setAdapter(adapter);

        //onItemClick
        lv_memershipcenter.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                for (int j = 0; j <listDatas.size() ; j++) {
                    if (i == j) {
                        listDatas.get(j).setSelect(true);
                    }else {
                        listDatas.get(j).setSelect(false);
                    }
                }
                amount = listDatas.get(i).getPrice();
                tv_membershipcenter_amount.setText("¥ " + Integer.valueOf(amount)/100);
                goodsName = listDatas.get(i).getCode();
                adapter.notifyDataSetChanged();
            }
        });
    }
    private String handleFen2Yuan(String fen) {
        if (fen.equals("0")) {
            return "0.00";
        }
        double srcFen = Double.valueOf(fen);
        double roundSrcFen = Math.round(srcFen);
        return String.valueOf(roundSrcFen/100);
    }


    @OnClick({R.id.rl_membershipcenter_picc,R.id.rl_membershipcenter_rate,R.id.btn_membershipcenter_payment_confirm})
    public void clickEvent(View view){

        switch (view.getId()) {

            //picc intruction
            case R.id.rl_membershipcenter_picc:
                //startActivity(PiccIntructionActivity.class);
                break;

            //memerreate intruction
            case R.id.rl_membershipcenter_rate:
                //startActivity(MemberRateIntruction.class);
                break;

            //确定
            case R.id.btn_membershipcenter_payment_confirm:

                createOrder();

                break;
        }


    }




    private void createOrder() {

        progressDialogUtils.showProgress();

        //构造传输参数
        final Map<String, String> map = new HashMap<String,String>();
        map.put("linkId",linkId);
        map.put("payType","1");
        map.put("orderType","10");
        map.put("amount",amount);
        map.put("goodsName",goodsName);
        map.put("product", ConstantsPay.product1001);

        //向服务器发送请求
        HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.PayOrderCreate, map, mQueue, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {
                progressDialogUtils.dismissProgress();
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    Intent intent = new Intent(mContext,OrderConfirmBuyMembershipActivity.class);
                    intent.putExtra("amount",amount);
                    intent.putExtra("bindCards",jsonObject.optString("bindCards"));
                    intent.putExtra("orderNo",jsonObject.optString("orderNo"));
                    intent.putExtra("srcActivity","MembershipCenterActivity");
                    startActivity(intent);

                } catch (JSONException e) {
                    e.printStackTrace();
                    AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.json_parse_error));
                }
            }

            @Override
            public void onError(VolleyError volleyError) {
                progressDialogUtils.dismissProgress();
                AlertDialogUtils.showAlertDialog(mContext, volleyError.getMessage());
            }
        });


    }




}
